#coding=utf-8
# !/usr/bin/python
# -*- coding: utf-8 -*-

from com import * 
import struct
from PySide6.QtGui import QPalette, QColor


class Devsta:
    def __init__(self, ui):
        self.ui = ui

    def ui_callback1(self):
        
        cstr2 = ["停止", "启动"]
        if(self.power > len(cstr2)):
            self.power = 0
        self.ui.lineEdit_241.setText(cstr2[self.power])

        self.ui.lineEdit_242.setText("{:.1f}%".format(self.o2p/10))
        self.ui.lineEdit_243.setText("{:.1f}L".format(self.o2fiow/10))
        self.ui.lineEdit_244.setText("{:.1f}kPa".format(self.o2press/10))

        self.ui.lineEdit_245.setText("{:.0f}mA".format(self.fval1_current))
        self.ui.lineEdit_246.setText("{:.0f}mA".format(self.fval2_current))
        self.ui.lineEdit_247.setText("{:.0f}mA".format(self.dval_current))
        self.ui.lineEdit_248.setText("{:.1f}Pa".format(self.sample_pe/10))
        self.ui.lineEdit_249.setText("{:.0f}米".format(self.altitude))
        self.ui.lineEdit_250.setText("{:.1f}kPa".format(self.airpress/10))
        self.ui.lineEdit_251.setText("{:.1f}Pa".format(self.o2p2/10))

        # self.ui.lineEdit_262.setText("{:X}".format(self.hw_version))
        sf_version = "{:X}.{:X}.{:X}.{:X}".format((self.sf_version >> 12)&0xF,\
                (self.sf_version >> 8)&0xF,(self.sf_version >> 4)&0xF,(self.sf_version >> 0)&0xF)
        self.ui.lineEdit_263.setText(sf_version)

        sf_date = "{:0>2d}年{:0>2d}月{:0>2d}日".format((self.sf_date >> 9),\
                (self.sf_date >> 5)&0xF, (self.sf_date >> 0)&0x1F)

        self.ui.lineEdit_264.setText(sf_date)
        self.ui.lineEdit_265.setText("{:X}".format(self.cfg_crc16))

        t = int(self.sys_power_on_c)
        str6 = "{:d}:".format(int(t/3600)) + "{:0>2d}:".format(int(t/60%60)) + "{:0>2d}".format(int(t%60))
        self.ui.lineEdit_252.setText(str6) # 上电时间

        self.ui.lineEdit_262.setText("{:.0f}小时".format(self.t_siver)) # 分子筛剩余时间
        self.ui.lineEdit_266.setText("{:.0f}小时".format(self.t_filter)) # 过滤器剩余时间

    def ui_callback103(self):
        nodestr = [
            "供氧性能失效",
            "氧浓度低",
            "分子筛寿命到期",
            "过滤器寿命到期",
            "压缩机过温/过流",
            "压力传感器故障",
            "超声波传感器故障",
            "分子筛阀故障",
            "出气阀故障",
            "压差传感器故障"
        ]
        resstr = [
            ":正常",
            ":故障",
        ]

        ui_list = [self.ui.lineEdit_288, self.ui.lineEdit_284,
            self.ui.lineEdit_285, self.ui.lineEdit_293,
            self.ui.lineEdit_287, self.ui.lineEdit_255,
            self.ui.lineEdit_253, self.ui.lineEdit_281,
            self.ui.lineEdit_286, self.ui.lineEdit_259]
        err_colour = QColor(255, 64, 0) 
        ok_colour = QColor(0, 180, 0) 
        for i in range(len(ui_list)):
            uinode = ui_list[i]
            fault = get_bit(self.dzyt_fault, i)
            fault_str = nodestr[i] + resstr[fault]
            palette = uinode.palette()
            if fault:
                palette.setColor(QPalette.Text, err_colour)
            else:
                palette.setColor(QPalette.Text, ok_colour)
            uinode.setPalette(palette)    
            uinode.setText(fault_str)

        self.ui.lineEdit_291.setText("压缩机PWM{:.0f}%".format(self.motor_pwm))
        self.ui.lineEdit_290.setText("压缩机转速{:.0f}".format(self.motor_rpm))
        self.ui.lineEdit_254.setText("压缩机电压{:.1f}V".format(self.motor_v/10))
        self.ui.lineEdit_257.setText("压缩机电流{:.1f}A".format(self.motor_a/10))
        self.ui.lineEdit_258.setText("压缩机功率{:.0f}W".format(self.motor_p))
        self.ui.lineEdit_256.setText("驱动板温度{:.0f}℃".format(self.motor_temp1))
        # self.ui.lineEdit_292.setText("温度2{:.0f℃}".format(self.motor_temp2))
        # self.ui.lineEdit_289.setText("{:.0f}".format(0))
        # self.ui.lineEdit_283.setText("{:.0f}".format(0))
        # self.ui.lineEdit_282.setText("{:.0f}".format(0))
        
        
    def decode_sta(self, a):
        self.power = struct.unpack('<B', a[7:8])[0]
        dzyt_fault = struct.unpack('<B', a[8:9])[0] # 8 氧调故障状态
        dzyt_weihu = struct.unpack('<B', a[9:10])[0] # 9 氧调维护告警
        self.dzyt_fault = (dzyt_fault & 0xF) + (dzyt_weihu << 4)

        self.o2p = struct.unpack('<h', a[10:12])[0] # 10 氧浓度
        self.o2fiow = struct.unpack('<h', a[12:14])[0] # 17 
        self.o2press = struct.unpack('<h', a[14:16])[0] # 19 供氧阀1校准系数
        self.fval1_current = struct.unpack('<h', a[16:18])[0] # 21 供氧阀2校准系数
        self.fval2_current = struct.unpack('<h', a[18:20])[0] #24 压差1
        self.dval_current = struct.unpack('<h', a[20:22])[0] #26 压差传感器1校准系数
        self.sample_pe = struct.unpack('<h', a[22:24])[0] #28 压差传感器1标准差
        self.altitude = struct.unpack('<h', a[24:26])[0] #30 压差传感器1电压
        self.airpress = struct.unpack('<h', a[26:28])[0] #32 压差2
        self.o2p2 = struct.unpack('<h', a[28:30])[0] #32 压差2
        
        self.motor_pwm = struct.unpack('<h', a[32:34])[0] #34 压差传感器2校准系数
        self.motor_rpm = struct.unpack('<h', a[34:36])[0] #36 压差传感器2标准差
        self.motor_v = struct.unpack('<h', a[36:38])[0] #38 压差传感器2电压
        self.motor_a = struct.unpack('<h', a[38:40])[0] #40 供氧阀1电流
        self.motor_p = struct.unpack('<h', a[40:42])[0] #40 供氧阀1电流
        self.motor_temp1 = struct.unpack('<h', a[42:44])[0] #42 供氧阀2电流
        
        self.sys_power_on_c = struct.unpack('<h', a[44:46])[0] #44 
        print(self.sys_power_on_c)
        
        self.sf_version = struct.unpack('>H', a[52:54])[0] #52 软件版本信息
        self.sf_date = struct.unpack('<H', a[54:56])[0] #54 软件日期信息
        self.cfg_crc16 = struct.unpack('<H', a[56:58])[0] #56 配置校验值
        self.t_siver = struct.unpack('<H', a[58:60])[0] #58 分子筛剩余时间
        self.t_filter =  struct.unpack('<H', a[60:62])[0] #58 过滤器剩余时间
        self.ui_callback1()
        self.ui_callback103()
     


